﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Specialized;
using System.Web.Routing;
using System.Web.Mvc;
using System.ComponentModel;
using ECube.Web.CMS.Extensions;

namespace ECube.Web.CMS.Models
{
    /// <summary>
    /// MvcSiteMapNode
    /// </summary>
    public class MvcSiteMapNode : SiteMapNode
    {
        #region Fields

        /// <summary>
        /// Url
        /// </summary>
        protected string url;

        /// <summary>
        /// Title
        /// </summary>
        protected string title;
        private string _ParentKey;
        #endregion

        #region Constructor

        ///// <summary>
        ///// Creates a new MvcSiteMapNode instance
        ///// </summary>
        //public MvcSiteMapNode(SiteMapProvider provider, string key)
        //    : base(provider, key)
        //{
        //    IsDynamic = false;
        //    DynamicParameters = new List<string>();
        //    Visibility = MvcSiteMapNodeVisibility.Full;
        //}

        /// <summary>
        /// Creates a new MvcSiteMapNode instance
        /// </summary>
        //public MvcSiteMapNode(SiteMapProvider provider, string key, string parentkey,NameValueCollection explicitResourceKeys)
        //    : base(provider, key, "", "", "", new ArrayList(), new NameValueCollection(),explicitResourceKeys, "")
        public MvcSiteMapNode(SiteMapProvider provider, string key, NameValueCollection explicitResourceKeys)
            : base(provider, key, "", "", "", new ArrayList(), new NameValueCollection(),explicitResourceKeys, "")
        {
            IsDynamic = false;
            DynamicParameters = new List<string>();
            Visibility = MvcSiteMapNodeVisibility.Full;
            
            url = "~/";
            //ParentKey = parentkey;
            //RouteValues = new Dictionary<string, object>();
            AllowAnonymous = true;
        }
        #endregion

        #region Properties

        public String PageId { get; set; }

        public bool IsMvcAction { get; set; }
        /// <summary>
        /// Area name the SiteMap node is linked to
        /// </summary>
        public string Area { get; set; }

        /// <summary>
        /// Controller name the SiteMap node is linked to
        /// </summary>
        public string Controller { get; set; }

        /// <summary>
        /// Action method name the SiteMap node is linked to
        /// </summary>
        public string Action { get; set; }

        /// <summary>
        /// Sets the current SiteMap node as dynamic
        /// </summary>
        public bool IsDynamic { get; set; }

        /// <summary>
        /// Parameters to ignore when IsDynamic is true 
        /// </summary>
        public List<string> DynamicParameters { get; private set; }

        /// <summary>
        /// Key of parent SiteMap node
        /// </summary>
        public string ParentKey
        {
            get
            {
                if (this.ParentNode != null)
                {
                    return this.ParentNode.Key;
                }
                return _ParentKey;
            }
            set
            {
                _ParentKey = value;
            }
        }

        /// <summary>
        /// Optional imageUrl
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// MvcSiteMapNode visibility 
        /// </summary>
        public MvcSiteMapNodeVisibility Visibility { get; set; }

        /// <summary>
        /// Route values
        /// </summary>
        public IDictionary<string, object> RouteValues { get; set; }

        /// <summary>
        /// Url
        /// </summary>
        public override string Url
        {
            get
            {
                if (!this.IsMvcAction)
                {
                    return this.url;
                }
                else  if (RouteValues != null)
                {
                    RequestContext ctx;
                    if (HttpContext.Current.Handler is MvcHandler)
                        ctx = ((MvcHandler)HttpContext.Current.Handler).RequestContext;
                    else
                        ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData());

                    return new UrlHelper(ctx).Action(Action, Controller, new RouteValueDictionary(RouteValues));
                }
                return null;
            }
            set
            {
                this.url = value;
            }
        }


        /// <summary>
        /// Title
        /// </summary>
        [Localizable(true)]
        public override string Title
        {
            get
            {
                if (this.Provider.EnableLocalization)
                {
                    string implicitResourceString = this.GetImplicitResourceString("title");
                    if (implicitResourceString != null && implicitResourceString == this.title)
                    {
                        return implicitResourceString;
                    }
                    implicitResourceString = this.GetExplicitResourceString("title", this.title, true);
                    if (implicitResourceString != null && implicitResourceString == this.title)
                    {
                        return implicitResourceString;
                    }
                }
                if (this.title != null)
                {
                    return this.title;
                }
                return string.Empty;
            }
            set
            {
                this.title = value;
            }
        }

        #endregion


        public int Level
        {
            get
            {
                return this.GetNodeLevel();                
            }
        }

        public String Group { get; set; }

        public bool AllowAnonymous { get; set; }
    }
}
